{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法一：容易超时\n",
    "class Solution:\n",
    "    def numTrees(self, n: int) -> int:\n",
    "        if(n == 0):\n",
    "            return 1\n",
    "        cnt = 0\n",
    "        for i in range(1, n+1):\n",
    "            l = self.numTrees(i-1)\n",
    "            r = self.numTrees(n-i)\n",
    "            cnt += l*r\n",
    "        return cnt\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法二：直接上公式（卡特兰数）\n",
    "import math\n",
    "f = math.factorial\n",
    "class Solution:\n",
    "    def numTrees(self, n: int) -> int:\n",
    "        return f(2*n)//(f(n)**2)//(n+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法三：动态规划\n",
    "class Solution:\n",
    "    def numTrees(self, n: int) -> int:\n",
    "        dp = [1,1]\n",
    "        for i in range(2, n+1):\n",
    "            dp.append(0)\n",
    "            for j in range(i):\n",
    "                dp[-1] += dp[j-0]*dp[i-1-j]\n",
    "        return dp[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1767263190\n"
     ]
    }
   ],
   "source": [
    "a = Solution()\n",
    "print(a.numTrees(19))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "总结了一下卡特兰数，有错误欢迎指正。\n",
    "\n",
    "分析一下本题：\n",
    "\n",
    "假设由 n 个结点组成的不同的二叉搜索树数量为 h(n)。\n",
    "那么现在再假设将第 k 个结点作为根节点，可以得到 f(k)=h(k-1)*h(n-k)。\n",
    "而每个结点都可能会被作为根节点，也就是说 k 的取值范围是从 1 到 n 的。\n",
    "所以可以推出公式：h(n) = f(1) + f(2) + ... + f(n)。\n",
    "进而满足了卡特兰数递推通式：h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (n>=2)。\n",
    "所以本题可以使用卡特兰数进行计算！\n",
    "\n",
    "补充一下，为了计算的方便，我们可以选用以下两个公式任意一个：\n",
    "\n",
    "C0 = 1，Cn+1 = Cn * (4n + 2) / (n + 2)\n",
    "Cn = (2n)! / ((n + 1)! * n !)\n",
    "本题选第一个公式简单一些，第二个虽然可以直接得到结果，但阶乘要自己写。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
